package com.sjx.lib_weather.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.sjx.lib_base.com.sjx.lib_base.view.TemperatureLine
import com.sjx.lib_base.utils.WeatherUtil
import com.sjx.lib_network.bean.HourlyData
import com.sjx.lib_weather.R
import java.text.SimpleDateFormat
import java.util.Locale

/**
 * FileName: HourlyAdapter
 * Founder: sjx
 * Profile: 逐小时温度适配器
 */
class HourlyAdapter(private val mHourlyList: List<HourlyData>) :
    RecyclerView.Adapter<HourlyAdapter.HourlyHolder>() {


    inner class HourlyHolder(itemView: View) : ViewHolder(itemView) {
        val tvTime: TextView = itemView.findViewById(R.id.tv_time)
        val ivIcon: ImageView = itemView.findViewById(R.id.iv_weather_state)
        val tvTemperature: TextView = itemView.findViewById(R.id.tv_temperature)
        val temperatureLine: TemperatureLine = itemView.findViewById(R.id.temperature_line)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        HourlyHolder(
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_weather_hourly_list, parent, false)
        )

    override fun getItemCount() = mHourlyList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: HourlyHolder, position: Int) {
        val hourlyData: HourlyData = mHourlyList[position]
        holder.tvTime.text = convertDateTimeFormat(hourlyData.fxTime)
        holder.tvTemperature.text = hourlyData.temp + "℃"
        WeatherUtil.changeIcon(holder.ivIcon, hourlyData.icon.toInt())
        mHourlyList.getMinMaxTemp()?.let { (minTemp, maxTemp) ->
            val currentTemp = mHourlyList[position].temp.toInt()
            val prevTemp = mHourlyList.getOrElse(position - 1) { null }?.temp?.toInt()
            val nextTemp = mHourlyList.getOrElse(position + 1) { null }?.temp?.toInt()
            holder.temperatureLine.setTemperatureValue(
                maxTemp,
                minTemp,
                currentTemp,
                prevTemp,
                nextTemp
            )
        }

    }

    // 获取最大值和最小值的扩展函数
    private fun List<HourlyData>.getMinMaxTemp(): Pair<Int, Int>? {
        if (isEmpty()) return null

        var minTemp = this[0].temp
        var maxTemp = this[0].temp

        for (hourlyData in this) {
            if (hourlyData.temp < minTemp) {
                minTemp = hourlyData.temp
            }
            if (hourlyData.temp > maxTemp) {
                maxTemp = hourlyData.temp
            }
        }

        return Pair(minTemp.toInt(), maxTemp.toInt())
    }

    private fun convertDateTimeFormat(inputDateTime: String): String? {
        val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mmXXX", Locale.getDefault())
        val outputFormat = SimpleDateFormat("HH:mm", Locale.getDefault())

        try {
            val date = inputFormat.parse(inputDateTime)
            return date?.let { outputFormat.format(it) }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return ""
    }


}

